package main;

import java.util.List;

import Nhom7.mainPackage.main.R;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class ViewMap extends MapActivity {
	private MapView mapView;
	private MapController mc;
	private LocationManager locationManager;
	private GeoPoint p;
	private Bundle extras;
	private Double latiTude;
	private Double longiTude;
	private GeoPoint point2;
	private ImageButton backMain;
	private ImageButton viewMenu;
	private String name;
	private int maDuLieu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_map);
		mapView = (MapView) findViewById(R.id.simplemap);
		backMain = (ImageButton) findViewById(R.id.toMainFromMap);
		backMain.setImageResource(R.drawable.go_previous_blue);
		backMain.setOnClickListener(new backToMain());
		viewMenu = (ImageButton) findViewById(R.id.buttonViewMenufromMap);
		viewMenu.setImageResource(R.drawable.menu);
		viewMenu.setOnClickListener(new viewMenuBtnClick());
		switch (main.selectedItem.MaDichVu()) {
		case 3:
		case 4:
		case 9:
		case 11:
			viewMenu.setEnabled(true);
			break;
		default:
			viewMenu.setEnabled(false);
		}

		locationManager = (LocationManager) this
				.getSystemService(Context.LOCATION_SERVICE);
		String provider = LocationManager.GPS_PROVIDER;
		Location loca = locationManager.getLastKnownLocation(provider);
		List<Overlay> mapOverlays = mapView.getOverlays();

		Drawable drawable = this.getResources().getDrawable(R.drawable.map1);
		HelloOverlay itemizedoverlay = new HelloOverlay(drawable, this);
		HelloOverlay itemizedoverlay1 = new HelloOverlay(drawable, this);

		extras = getIntent().getExtras();
		name = extras.getString("Ten");
		maDuLieu = extras.getInt("DuLieu");
		latiTude = extras.getDouble("Vi_Do");
		longiTude = extras.getDouble("Kinh_Do");

		p = new GeoPoint((int) (latiTude * 1E6), (int) (longiTude * 1E6));

		OverlayItem overlayitem = new OverlayItem(p, "Hola, Mundo!",
				"I'm in Mexico City!");
		point2 = new GeoPoint((int) (10.345673 * 1E6), (int) (106.434322 * 1E6));
		LocationListener locationListener = new LocationListener() {

			public void onStatusChanged(String provider, int status,
					Bundle extras) {
				// TODO Auto-generated method stub

			}

			public void onProviderEnabled(String provider) {
				// TODO Auto-generated method stub

			}

			public void onProviderDisabled(String provider) {
				// TODO Auto-generated method stub

			}

			public void onLocationChanged(Location location) {
				// TODO Auto-generated method stub
				point2 = new GeoPoint((int) (location.getLatitude() * 1E6),
						(int) (location.getLongitude() * 1E6));
			}
		};

		locationManager
				.requestLocationUpdates(provider, 0, 0, locationListener);
		OverlayItem overlayitem2 = new OverlayItem(point2, "Sekai, konichiwa!",
				"I'm in Japan!");
		mc = mapView.getController();
		mc.animateTo(p);
		mc.setZoom(13);
		mapView.invalidate();
		itemizedoverlay.addOverlay(overlayitem);
		itemizedoverlay1.addOverlay(overlayitem2);
		mapView.getOverlays().add(itemizedoverlay);
		mapView.getOverlays().add(itemizedoverlay1);

		mapView.setBuiltInZoomControls(true);
		mapView.postInvalidate();
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	class viewMenuBtnClick implements OnClickListener {
		public void onClick(View v) {
			viewMenu();
		}

	}

	public void viewMenu() {
		Intent i = new Intent(this, ViewMenu.class);
		i.putExtra("Ten", name);
		i.putExtra("DuLieu", maDuLieu);
		i.putExtra("Vi_Do", latiTude);
		i.putExtra("Kinh_Do", longiTude);
		startActivityForResult(i, 1);
	}

	class backToMain implements OnClickListener {
		public void onClick(View v) {
			viewMain();
		}
	}

	public void viewMain() {
		Intent i = new Intent(this, main.class);
		startActivityForResult(i, 1);
	}
}
